<template>
  <div v-loading="loading">
    <div class="text-right mb-2">
      <el-button icon="el-icon-printer" @click="handlePrint">打印</el-button>
      <el-button icon="el-icon-download" @click="exportDocx" :loading="exportLoading">
        导出
      </el-button>
    </div>
    <div :id="printId">
      <table-base title="欠税信息" :data="taxData" :columns="taxColumns"></table-base>
      <table-base
        title="税务行政处罚"
        :data="taxPunishData"
        :columns="taxPunishColumns"
      ></table-base>
      <table-base title="黑名单信息" :data="blackListData" :columns="blackListColumns"></table-base>
      <table-base
        title="严重违法失信名单"
        :data="blackIllegalData"
        :columns="blackIllegalColumns"
      ></table-base>
      <table-base
        title="行政处罚和其他部门行政处罚"
        :data="adminIllegalData"
        :columns="adminIllegalColumns"
      ></table-base>
      <table-base
        title="资产抵质押详情信息"
        :data="assetPledgeData"
        :columns="assetPledgeColumns"
      ></table-base>
      <table-base
        title="企业存在土地抵押"
        :data="landMortgateData"
        :columns="landMortgateColumns"
      ></table-base>
      <table-base
        title="对外担保信息"
        :data="externalGuaData"
        :columns="externalGuaColumns"
      ></table-base>
      <table-base
        title="工商变更信息 股东或股权"
        :data="businessChangeData"
        :columns="businessChangeColumns"
      ></table-base>
      <table-base title="婚姻信息" :data="marriedData" :columns="marriedColumns"></table-base>
    </div>
  </div>
</template>

<script>
import TableBase from '@/credit/views/city/program/black/components/TableBase'
import { printA4, docxTemplateExport } from '@vimi/utils-tool'
export default {
  components: {
    TableBase,
  },
  props: {
    queryId: String,
  },
  data() {
    return {
      loading: false,
      tableName: 'scan_black_record',
      exportLoading: false,
      tableData: {},
      // 一、欠税信息
      taxColumns: [
        { label: '欠税公告日期', prop: 'qsggrq' },
        { label: '公告税务局', prop: 'ggswj' },
        { label: '欠税税种', prop: 'qssz' },
        { label: '法定代表人（负责人或业主姓名)', prop: 'fzrxm' },
        { label: '经营地点', prop: 'jydz' },
        { label: '管理单位', prop: 'gldw' },
        { label: '当期新发生的欠税金额', prop: 'dqxzqsj' },
        { label: '欠税余额', prop: 'qsye' },
      ],
      // 二、税务行政处罚
      taxPunishColumns: [
        { label: '处罚生效期', prop: 'cfsxq' },
        { label: '处罚截至期', prop: 'cfjzq' },
        { label: '处罚机关', prop: 'swjgmc' },
        { label: '行政处罚决定书文号', prop: 'wh' },
        { label: '法定代表人姓名', prop: 'fddbrxm' },
        { label: '处罚结果', prop: 'cfjg' },
        { label: '当前状态', prop: 'dqzt' },
      ],
      // 三、黑名单信息
      blackListColumns: [
        { label: '发布日期', prop: 'fb_date' },
        { label: '发布单位', prop: 'fb_dw' },
        { label: '红黑名单类别名称', prop: 'hhmd_type_name' },
        { label: '红黑名单名称', prop: 'hhmd_name' },
        { label: '法定代表人名称', prop: 'fddbr_name' },
        { label: '发布决定文书号', prop: 'fbjdwsh' },
        { label: '发布决定内容', prop: 'fbjdnr' },
        { label: '发布决定单位', prop: 'fbjddw' },
        { label: '信用行为', prop: 'xyxw' },
      ],
      // 四、严重违法失信名单
      blackIllegalColumns: [
        { label: '列入日期', prop: 'abntime' },
        { label: '列入作出决定机关', prop: 'decorg' },
        { label: '列入文号', prop: 'dedocnum' },
        { label: '法定代表人', prop: 'name' },
        { label: '列入事由/情形', prop: 'serillrea_cn' },
        { label: '列入作出决定机关(中文名称)', prop: 'decorg_cn' },
      ],
      // 五、行政处罚和其他部门行政处罚 admin_illegal
      adminIllegalColumns: [
        { label: '公示日期', prop: 'publicdate' },
        { label: '处罚决定书文号', prop: 'pendecno' },
        { label: '违法行为类型', prop: 'illegacttype' },
        { label: '处罚内容', prop: 'pencontent' },
        { label: '作出行政处罚决定机关', prop: 'judauth' },
        { label: '作出行政处罚决定机关（中文名称）', prop: 'pentype_cn' },
      ],
      // 六、资产抵质押详情信息
      assetPledgeColumns: [
        { label: '登记日期', prop: 'regidate' },
        { label: '登记编号', prop: 'morregcno' },
        { label: '登记机关（中文名称）', prop: 'regorg_cn' },
        { label: '状态', prop: 'type' },
        { label: '抵押人证件类型', prop: 'mort_doctype' },
        { label: '抵押人证件号码', prop: 'mort_docid' },
        { label: '注销日期', prop: 'candate' },
        { label: '注销原因（中文名称）', prop: 'morcanrea_cn' },
        { label: '注销原因', prop: 'mortgagor' },
      ],
      // 七、企业存在土地抵押
      landMortgateColumns: [
        { label: '坐落', prop: 'zjjzwzl' },
        { label: '权利性质', prop: 'qlxzname' },
        { label: '抵押人名称', prop: 'qlrmc' },
        { label: '他项权证号', prop: 'bdcdjzmh' },
        { label: '债务履行起始时间', prop: 'zwlxqssj' },
        { label: '债务履行结束时间', prop: 'zwlxjssj' },
        { label: '抵押金额', prop: 'dyje' },
        { label: '抵押面积', prop: 'dyztdmj' },
      ],
      // 八、对外担保信息
      externalGuaColumns: [
        { label: '对外担保标识ID', prop: 'moreid' },
        { label: '债权人', prop: 'more' },
        { label: '债务人', prop: 'mortgagor' },
        { label: '主债权种类', prop: 'priclaseckind' },
        { label: '主债权数额', prop: 'priclasecam' },
        { label: '履行债务的期限自', prop: 'pefperform' },
        { label: '履行债务的期限至', prop: 'pefperto' },
        { label: '保证的期间', prop: 'guaranperiod' },
        { label: '对外担保是否公示', prop: 'moredis' },
        { label: '保证的方式', prop: 'gatype' },
      ],
      // 九、工商变更信息 股东或股权
      businessChangeColumns: [
        { label: '变更项ID', prop: 'altid' },
        // { label: '变更事项代码', prop: 'altitem' },
        { label: '变更事项（中文名称)', prop: 'altitem_cn' },
        { label: '变更前内容', prop: 'altbe' },
        { label: '变更后内容', prop: 'altaf' },
        { label: '变更日期', prop: 'altdate' },
      ],

      // 十、婚姻信息
      marriedColumns: [
        { label: '姓名', prop: 'user_name' },
        { label: '身份证号', prop: 'id_number' },
        { label: '婚姻状态', prop: 'optype' },
      ],
    }
  },
  computed: {
    taxData() {
      return (this.tableData.scan_black_tax || []).sort((a, b) => (b.qsggrq < a.qsggrq ? -1 : 1))
    },
    taxPunishData() {
      return (this.tableData.scan_black_tax_punish || []).sort((a, b) =>
        b.cfsxq < a.cfsxq ? -1 : 1
      )
    },
    blackListData() {
      return (this.tableData.scan_black_black_list || []).sort((a, b) =>
        b.fb_date < a.fb_date ? -1 : 1
      )
    },
    blackIllegalData() {
      return (this.tableData.scan_black_illegal || []).sort((a, b) =>
        b.abntime < a.abntime ? -1 : 1
      )
    },
    adminIllegalData() {
      return (this.tableData.scan_black_admin_illegal || []).sort((a, b) =>
        b.publicdate < a.publicdate ? -1 : 1
      )
    },
    assetPledgeData() {
      return (this.tableData.scan_black_asset_pledge || []).sort((a, b) =>
        b.regidate < a.regidate ? -1 : 1
      )
    },
    landMortgateData() {
      return (this.tableData.scan_black_land_mortgate || []).sort((a, b) =>
        b.zwlxqssj < a.zwlxqssj ? -1 : 1
      )
    },
    externalGuaData() {
      return (this.tableData.scan_black_external_gua || []).sort((a, b) =>
        b.pefperform < a.pefperform ? -1 : 1
      )
    },
    businessChangeData() {
      return (this.tableData.scan_black_business_change || []).sort((a, b) =>
        b.altdate < a.altdate ? -1 : 1
      )
    },
    marriedData() {
      return this.tableData.scan_black_married || []
    },
    printId() {
      return `black-${this.queryId}`
    },
  },
  watch: {
    queryId: {
      handler: function () {
        this.getData()
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    getData() {
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, {
          'customer_related_id.eq': this.queryId,
          subKey:
            'scan_black_tax.scan_id,scan_black_tax_punish.scan_id,scan_black_black_list.scan_id,scan_black_illegal.scan_id,scan_black_admin_illegal.scan_id,scan_black_asset_pledge.scan_id,scan_black_land_mortgate.scan_id,scan_black_external_gua.scan_id,scan_black_business_change.scan_id,scan_black_married.scan_id',
        })
        .then(res => {
          this.tableData = res?.data?.[0] ?? {}
        })
        .finally(() => {
          this.loading = false
        })
    },
    handlePrint() {
      printA4(document.getElementById(this.printId).innerHTML)
    },
    exportDocx() {
      this.exportLoading = true
      const fileName = '扫黑结果'
      const templaterUrl = '/credit/files/扫黑结果.docx'
      const tags = {
        taxData: this.taxData,
        taxPunishData: this.taxPunishData,
        blackListData: this.blackListData,
        blackIllegalData: this.blackIllegalData,
        adminIllegalData: this.adminIllegalData,
        assetPledgeData: this.assetPledgeData,
        landMortgateData: this.landMortgateData,
        externalGuaData: this.externalGuaData,
        businessChangeData: this.businessChangeData,
        marriedData: this.marriedData,
      }
      docxTemplateExport({
        templaterUrl,
        tags,
        fileName,
      })
        .then(() => {
          this.$message.success('导出成功')
        })
        .catch(() => {
          this.$message.error('导出失败')
        })
        .finally(() => {
          this.exportLoading = false
        })
    },
  },
}
</script>

<style lang="scss" scoped>
/deep/ .company-section-title {
  font-weight: bold;
  padding-left: 1rem;
  position: relative;
  margin-bottom: 1rem;
  font-size: 1.25rem;

  &::before {
    content: '';
    width: 5px;
    height: 16px;
    background: var(--primary);
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 2px;
  }
}
</style>
